package com.liu.service.impl;


import com.liu.constant.RedisConstant;
import com.liu.error.BusinessException;
import com.liu.mapper.StockMapper;
import com.liu.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品service
 */
@Service
public class StockServiceImpl implements StockService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StockMapper stockMapper;

//    @Autowired
//    private MqProvider mqProvider;

    @Override
    @Transactional
    public boolean decreaseStock(Long goodsId, Integer amount) throws BusinessException {

        Long num = redisTemplate.opsForValue().decrement(RedisConstant.goodsStockDecre + goodsId, amount);
        if (num.compareTo(0L)>=0) {
            return true;
        } else {
            redisTemplate.opsForValue().increment(RedisConstant.goodsStockDecre + goodsId, amount);
            return false;
        }
    }

    @Override
    public boolean increaseStock(Long goodsId, Integer amount) throws BusinessException {

        return true;
    }
}